home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / CEGUI / CEGUIUDim.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-07  |  6.8 KB  |  162 lines

  1. /************************************************************************
  2.     filename:   CEGUIUDim.h
  3.     created:    Tue May 31 2005
  4.     author:     Paul D Turner <paul@cegui.org.uk>
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #ifndef _CEGUIUDim_h_
  25. #define _CEGUIUDim_h_
  26.  
  27. #include "CEGUIRect.h"
  28. #include "CEGUIVector.h"
  29.  
  30. // some macros to aid in the creation of UDims
  31. #define cegui_absdim(x)     UDim(0,(x))
  32. #define cegui_reldim(x)     UDim((x),0)
  33.  
  34.  
  35. // Start of CEGUI namespace section
  36. namespace CEGUI
  37. {
  38.     class CEGUIEXPORT UDim
  39.     {
  40.     public:
  41.         UDim() {}
  42.         UDim(float scale, float offset) : d_scale(scale), d_offset(offset) {}
  43.         ~UDim() {}
  44.  
  45.         float asAbsolute(float base) const    { return PixelAligned(base * d_scale) + d_offset; }
  46.         float asRelative(float base) const    { return (base != 0.0f) ? d_offset / base + d_scale : 0.0f; }
  47.  
  48.         UDim operator+(const UDim& other) const     { return UDim(d_scale + other.d_scale, d_offset + other.d_offset); }
  49.         UDim operator-(const UDim& other) const     { return UDim(d_scale - other.d_scale, d_offset - other.d_offset); }
  50.         UDim operator/(const UDim& other) const     { return UDim(d_scale / other.d_scale, d_offset / other.d_offset); }
  51.         UDim operator*(const UDim& other) const     { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
  52.  
  53.         const UDim& operator+=(const UDim& other)   { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
  54.         const UDim& operator-=(const UDim& other)   { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
  55.         const UDim& operator/=(const UDim& other)   { d_scale /= other.d_scale; d_offset /= other.d_offset; return *this; }
  56.         const UDim& operator*=(const UDim& other)   { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
  57.  
  58.         bool operator==(const UDim& other) const    { return d_scale == other.d_scale && d_offset == other.d_offset; }
  59.         bool operator!=(const UDim& other) const    { return !operator==(other); }
  60.  
  61.         float d_scale, d_offset;
  62.     };
  63.  
  64.  
  65.     class CEGUIEXPORT UVector2
  66.     {
  67.     public:
  68.         UVector2() {}
  69.         UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
  70.         ~UVector2() {}
  71.  
  72.         Vector2 asAbsolute(const Size& base) const    { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
  73.         Vector2 asRelative(const Size& base) const    { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
  74.  
  75.         UVector2 operator+(const UVector2& other) const     { return UVector2(d_x + other.d_x, d_y + other.d_y); }
  76.         UVector2 operator-(const UVector2& other) const     { return UVector2(d_x - other.d_x, d_y - other.d_y); }
  77.         UVector2 operator/(const UVector2& other) const     { return UVector2(d_x / other.d_x, d_y / other.d_y); }
  78.         UVector2 operator*(const UVector2& other) const     { return UVector2(d_x * other.d_x, d_y * other.d_y); }
  79.  
  80.         const UVector2& operator+=(const UVector2& other)   { d_x += other.d_x; d_y += other.d_y; return *this; }
  81.         const UVector2& operator-=(const UVector2& other)   { d_x -= other.d_x; d_y -= other.d_y; return *this; }
  82.         const UVector2& operator/=(const UVector2& other)   { d_x /= other.d_x; d_y /= other.d_y; return *this; }
  83.         const UVector2& operator*=(const UVector2& other)   { d_x *= other.d_x; d_y *= other.d_y; return *this; }
  84.         
  85.         bool operator==(const UVector2& other) const    { return d_x == other.d_x && d_y == other.d_y; }
  86.         bool operator!=(const UVector2& other) const    { return !operator==(other); }
  87.  
  88.         UDim d_x, d_y;
  89.     };
  90.  
  91.  
  92.     class CEGUIEXPORT URect
  93.     {
  94.     public:
  95.         URect() {}
  96.         
  97.         URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
  98.         
  99.         URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
  100.         {
  101.             d_min.d_x = left;
  102.             d_min.d_y = top;
  103.             d_max.d_x = right;
  104.             d_max.d_y = bottom;
  105.         }
  106.         
  107.         ~URect() {}
  108.     
  109.         Rect asAbsolute(const Size& base) const
  110.         {
  111.             return Rect(
  112.                     d_min.d_x.asAbsolute(base.d_width),
  113.                     d_min.d_y.asAbsolute(base.d_height),
  114.                     d_max.d_x.asAbsolute(base.d_width),
  115.                     d_max.d_y.asAbsolute(base.d_height)
  116.                 );
  117.         }
  118.  
  119.         Rect asRelative(const Size& base) const
  120.         {
  121.             return Rect(
  122.                     d_min.d_x.asRelative(base.d_width),
  123.                     d_min.d_y.asRelative(base.d_height),
  124.                     d_max.d_x.asRelative(base.d_width),
  125.                     d_max.d_y.asRelative(base.d_height)
  126.                 );
  127.         }
  128.  
  129.         const UVector2& getPosition() const     { return d_min; }
  130.         UVector2 getSize() const                { return d_max - d_min; }
  131.         UDim getWidth() const                   { return d_max.d_x - d_min.d_x; }
  132.         UDim getHeight() const                  { return d_max.d_y - d_min.d_y; }
  133.  
  134.         void setPosition(const UVector2& pos)
  135.         {
  136.             UVector2 sz(d_max - d_min);
  137.             d_min = pos;
  138.             d_max = d_min + sz;
  139.         }
  140.  
  141.         void setSize(const UVector2& sz)
  142.         {
  143.             d_max = d_min + sz;
  144.         }
  145.  
  146.         void setWidth(const UDim& w)        { d_max.d_x = d_min.d_x + w; }
  147.         void setHeight(const UDim& h)       { d_max.d_y = d_min.d_y + h; }
  148.  
  149.         void offset(const UVector2& sz)
  150.         {
  151.             d_min += sz;
  152.             d_max += sz;
  153.         }
  154.         
  155.         UVector2 d_min, d_max;
  156.     };
  157.  
  158. } // End of  CEGUI namespace section
  159.  
  160.  
  161. #endif  // end of guard _CEGUIUDim_h_
  162.